package com.szqhyxj.youxiangju.biz.merchantHome.shop.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.szqhyxj.youxiangju.R;
import com.szqhyxj.youxiangju.biz.merchantHome.bean.PosCommodity;
import com.szqhyxj.youxiangju.sys.components.LoadImageUtils;
import com.szqhyxj.youxiangju.sys.util.StringUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class CommoditeDeviceAdapter extends BaseAdapter{

    private final LayoutInflater from;
    private List<PosCommodity> devices;

    private OnItemClickListener listener;
    public CommoditeDeviceAdapter(Context context, List<PosCommodity> devices){
        from = LayoutInflater.from(context);
        this.devices = devices;
    }

    @Override
    public int getCount(){
        return devices.size();
    }

    @Override
    public Object getItem(int i){
        return devices.get(i);
    }

    @Override
    public long getItemId(int i){
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup){
        if(view == null){
            view = from.inflate(R.layout.list_commodity_device_item_layout, null);
            view.setTag(new ViewHolder(view));
        }
        ViewHolder viewHolder = (ViewHolder) view.getTag();
        PosCommodity device = devices.get(i);
        viewHolder.commodityName.setText(device.getPackageName());
        viewHolder.quantity.setText("/"+device.getDeviceNum()+"台");
        viewHolder.price.setText(StringUtils.initPrice(device.getPackagePrice()));
        LoadImageUtils.loadImage(viewHolder.icon, device.getSmallPic());
        viewHolder.buyNow.setOnClickListener(new OnBtnClickListener(i));
        return view;
    }

     class OnBtnClickListener implements View.OnClickListener{
        private int i;

        public OnBtnClickListener(int i){
            this.i = i;
        }

        @Override
        public void onClick(View view){
            listener.onClick(devices.get(i).getPackageId());
        }
    }



    public void setListener(OnItemClickListener listener){
        this.listener = listener;
    }

    static class ViewHolder{
        @BindView(R.id.icon)
        ImageView icon;
        @BindView(R.id.commodity_name)
        TextView commodityName;
        @BindView(R.id.price)
        TextView price;
        @BindView(R.id.buy_now)
        TextView buyNow;
        @BindView(R.id.quantity)
        TextView quantity;
        ViewHolder(View view){
            ButterKnife.bind(this, view);
        }
    }

    public interface OnItemClickListener{
         void onClick(String packageId);
    }

}
